package edu.fsu.cs.SHPE;

import java.net.ConnectException;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SpeakerListAdapter extends BaseAdapter {

	private final static String TAG = "SHPE.SpeakerListAdapter";
	private Context context;
	private SpeakerContainer speakerContainer;
	
	
	public SpeakerListAdapter(Context context, String url) throws ConnectException {
		this.context = context;
		speakerContainer = new SpeakerContainer(url);	
	}

	@Override
	public int getCount() {
		return speakerContainer.size();
	}

	@Override
	public Object getItem(int position) {
		return speakerContainer.get(position);
	}

	@Override
	public long getItemId(int position) {
		return speakerContainer.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Log.i(TAG, "position " + position);

		LayoutInflater inFlate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
		View v = inFlate.inflate(R.layout.speakeritem, null);
		TextView tmp;
		
		Speaker tmpSpeaker = speakerContainer.get(position);

		if(tmpSpeaker.getPhoto() != null){
			ImageView image = (ImageView)v.findViewById(R.id.imgPhoto);
			image.setImageDrawable(tmpSpeaker.getPhoto());
		}
		
		if(tmpSpeaker.getFirstName() != null){
			tmp = (TextView)v.findViewById(R.id.txtName);
			tmp.setText(tmpSpeaker.getFirstName() + " " + tmpSpeaker.getLastName());
		}
		
		if(tmpSpeaker.getOrganization() != null){
			tmp = (TextView)v.findViewById(R.id.txtOrganization);
			tmp.setText(tmpSpeaker.getOrganization());
		}
		
		if(tmpSpeaker.getTitle() != null){
			tmp = (TextView)v.findViewById(R.id.txtTitle);
			tmp.setText(tmpSpeaker.getTitle());
		}
		
		 
		return v;
	}

}
